﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using ArkEC.SEMs.BizObject.MLM;
using System.Data.OleDb;

namespace ArkEC.SEMs.DataAccess.MLM.Implement.Access
{
    /// <summary>
    /// 语言种类数据库操作接口实现类
    /// </summary>
    public class LanTypeDA_Acs : ILanTypeDA
    {
        /// <summary>
        /// 获取所有语言资源
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        public DataTable GetLanTypes(DbConnection conn, DbTransaction tran)
        {
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = @"select LRCode, LRType from LanType order by LanType_Id";

            DataSet ds = new DataSet();
            OleDbDataAdapter adp = new OleDbDataAdapter(cmd as OleDbCommand);
            adp.Fill(ds);

            return ds.Tables[0];
        }

        /// <summary>
        /// 获取所有语言资源表
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        public DataTable GetLanResTables(DbConnection conn, DbTransaction tran)
        {
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = @"select * from SysResTableSymbol order by Id";

            DataSet ds = new DataSet();
            OleDbDataAdapter adp = new OleDbDataAdapter(cmd as OleDbCommand);
            adp.Fill(ds);

            return ds.Tables[0];
        }

        /// <summary>
        /// 删除所有语言种类
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="tran"></param>
        public void Clear(DbConnection conn, DbTransaction tran)
        {
            string strSql = @"delete * from LanType";

            DbCommand cmd = conn.CreateCommand();
            cmd.Transaction = tran;
            cmd.CommandText = strSql;

            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 新增语言种类
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="tran"></param>
        /// <param name="obj"></param>
        public void Insert(DbConnection conn, DbTransaction tran, LanType obj)
        {
            string strSql = @"insert into LanResourceType 
                                          ( Id,
                                            LRType,
                                            IsDefault
                                           ) values
                                          ( @Id,
                                            @LRType,
                                            @IsDefault
                                           ) ";

            DbCommand cmd = conn.CreateCommand();
            cmd.Transaction = tran;

            //cmd.CommandText = "SELECT max(CGId) from LanResourceType";
            //object idObj = cmd.ExecuteScalar();
            //obj.CGId = idObj == System.DBNull.Value ? 1 : Convert.ToInt32(idObj) + 1;

            cmd.CommandText = strSql;
            OleDbParameter[] arrParams = new OleDbParameter[] { 
                                new OleDbParameter("@id", obj.Id),
                                new OleDbParameter("@LRType", obj.LRType),
                                new OleDbParameter("@IsDefault", obj.IsDefault)
			};
            cmd.Parameters.AddRange(arrParams);
            cmd.ExecuteNonQuery();
        }
    }
}
